<?php
App::uses('AppController', 'Controller');
/**
 * Studentsubjects Controller
 *
 * @property Studentsubject $Studentsubject
 */
class StudentsubjectsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Studentsubject->recursive = 0;
		$this->set('studentsubjects', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Studentsubject->id = $id;
		if (!$this->Studentsubject->exists()) {
			throw new NotFoundException(__('Invalid data'));
		}
		$this->set('studentsubject', $this->Studentsubject->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
		
		 if(isset($this->request->data["Studentsubject"]["user_id"])){
		        $useId =AuthComponent::user('id');		
		        //$useId ="1";				
				$this->request->data["Studentsubject"]["user_id"]=$useId;
				}
				
			$this->Studentsubject->create();
			if ($this->Studentsubject->save($this->request->data)) {
				$id=$this->Studentsubject->id;
				$this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
		$students = $this->Studentsubject->Student->find('list');
		$branches = $this->Studentsubject->Branch->find('list');
		$uniyears = $this->Studentsubject->Uniyear->find('list');
		$subjects = $this->Studentsubject->Subject->find('list');
		$users = $this->Studentsubject->User->find('list');
		$this->set(compact('students', 'branches', 'uniyears', 'subjects', 'users'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Studentsubject->id = $id;
		if (!$this->Studentsubject->exists()) {
			throw new NotFoundException(__('Invalid studentsubject'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		
		 if(isset($this->request->data["Studentsubject"]["user_id"])){
		       $useId =AuthComponent::user('id');		
		       // $useId ="1";				
				$this->request->data["Studentsubject"]["user_id"]=$useId;
				}
				
			if ($this->Studentsubject->save($this->request->data)) {
			      $this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		} else {
			$this->request->data = $this->Studentsubject->read(null, $id);
		}
		$students = $this->Studentsubject->Student->find('list');
		$branches = $this->Studentsubject->Branch->find('list');
		$uniyears = $this->Studentsubject->Uniyear->find('list');
		$subjects = $this->Studentsubject->Subject->find('list');
		$users = $this->Studentsubject->User->find('list');
		$this->set(compact('students', 'branches', 'uniyears', 'subjects', 'users'));
	   $this->render("add");
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Studentsubject->id = $id;
		if (!$this->Studentsubject->exists()) {
			throw new NotFoundException(__('Invalid studentsubject'));
		}
		if ($this->Studentsubject->delete()) {
		       $this->savelog($id);
				
			$this->Session->setFlash(__('Data has been deleted'), 'default',array("class"=>"mws-form-message success"));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
}
